<template>
  <div>
    <crumbs secondTitle="商品管理" thirdTitle="商品列表"></crumbs>

    <!-- 卡片视图 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            clearable
            @clear="loadGoodList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="loadGoodList"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addGoods">添加按钮</el-button>
        </el-col>
      </el-row>

      <!-- 表格区域 -->
      <el-table :data="goodsList" border stripe="">
        <el-table-column type="index" label="#"> </el-table-column>
        <el-table-column label="商品名称" prop="goods_name"></el-table-column>
        <el-table-column
          label="商品价格(元)"
          prop="goods_price"
          width="95px"
        ></el-table-column>
        <el-table-column
          label="商品重量"
          prop="goods_weight"
          width="70px"
        ></el-table-column>
        <el-table-column label="创建时间" prop="add_time" width="140px">
          <template slot-scope="scope">
            {{ scope.row.add_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="130px">
          <template slot-scope="{ row }">
            <el-button
              size="mini"
              icon="el-icon-edit"
              type="primary"
            ></el-button>
            <el-button
              size="mini"
              icon="el-icon-delete"
              type="danger"
              @click="removeGoods(row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <change
        :total="total"
        :queryParams="queryInfo"
        @size="handleSizeChange"
        @current="handleCurrentChange"
      ></change>
    </el-card>
  </div>
</template>

<script>
import { getUserList, deleteGoods } from '@/api/list'
export default {
  data() {
    return {
      // 查询的参数对象
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      total: 0,
      // 商品列表
      goodsList: []
    }
  },
  methods: {
    // 点击按钮 跳转商品增加界面
    addGoods() {
      this.$router.push('/goods/add')
    },
    // 根据 id 删除商品
    async removeGoods(id) {
      try {
        await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        const res = await deleteGoods(id)
        if (res.meta.status === 200) {
          this.$message.success('删除商品成功')
          this.loadGoodList()
        }
      } catch (error) {
        this.$message.info('已取消删除')
      }
    },
    // 监听 pagesize 改变的事件
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      this.queryInfo.pagesize = val
      this.loadGoodList()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      this.queryInfo.pagenum = val
      this.loadGoodList()
    },
    async loadGoodList() {
      const res = await getUserList(this.queryInfo)
      if (res.meta.status === 200) {
        // this.$message.success('获取商品列表成功')
        this.total = res.data.total
        this.goodsList = res.data.goods
      }
    }
  },
  mounted() {
    this.loadGoodList()
  }
}
</script>

<style lang="less" scoped></style>
